package com.example.crondemo.utils;


import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Component
@Data
public class CommonConfig {
    /**
     * ERP 配置
     */
    @Value("${wdt.prod.baseUrl}")
    private String erp_requestUrl;
    @Value("${wdt.prod.sid}")
    private String erp_sid;
    @Value("${wdt.prod.appkey}")
    private String erp_appkey;
    @Value("${wdt.prod.appsecret}")
    private String erp_appsecret;
    @Value("${wdt.prod.relativeUrl}")
    private String relativeUrl;
    /**
     *
     * @param relativeUrl   接口地址(例:/openapi2/aaa.php)
     * @param params        接口参数
     * @return  响应json
     */
    public String getResponse(String relativeUrl, Map<String, String> params) throws IOException {
        WdtClient wdtClient = new WdtClient(this.erp_sid, this.erp_appkey, this.erp_appsecret,this.erp_requestUrl);
        return wdtClient.execute(relativeUrl, params);
    }

    /**
     *
     * @param params        接口参数
     * @return  响应json
     */
    public String getResponse( Map<String, String> params) throws IOException {
        WdtClient wdtClient = new WdtClient(this.erp_sid, this.erp_appkey, this.erp_appsecret,this.erp_requestUrl);
        return wdtClient.execute(this.relativeUrl, params);
    }


    public static String unicodeToString(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            //group 6728
            String group = matcher.group(2);
            //ch:'木' 26408
            ch = (char) Integer.parseInt(group, 16);
            //group1 \u6728
            String group1 = matcher.group(1);
            str = str.replace(group1, ch + "");
        }
        return str;
    }
}
